/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.workhorse.persistence.jpa;

import com.google.inject.Inject;
import com.google.inject.Provider;
import org.workhorse.exec.Execution;
import org.workhorse.graph.node.Node;
import org.workhorse.persistence.BasePersistence;
import org.workhorse.persistence.PersistenceManager;
import org.workhorse.process.Process;

import javax.persistence.EntityManagerFactory;
import java.util.Collection;

/**
 * @author Brennan Spies
 *
 * <p>Implementation of the {@link org.workhorse.persistence.Persistence Persistence} interface that uses the JPA API.</p>
 */
public class JpaPersistence extends BasePersistence
{
    private final Provider<EntityManagerFactory> emfProvider;

    @Inject
    public JpaPersistence(Provider<EntityManagerFactory> emfProvider) {
        this.emfProvider = emfProvider;
    }

    /**
     * Starts the Persistence service.
     */
    public void start() { 
        final EntityManagerFactory emf = emfProvider.get();
        init(new Provider<PersistenceManager>(){
            public PersistenceManager get() {
                return new JpaPersistenceManager(emf);
            }
        });
    }

    /**
     * Ends the Persistence service.
     */
    public void shutdown() {
        emfProvider.get().close();
    }

    /**
     * Begins a unit of work.
     */
/*    public void beginWork() {
       EntityManager em = emBinding.get();
        if(em==null) {
            em = entityManagerFactory.createEntityManager();
            emBinding.bind(em);
        }
        em.getTransaction().begin();
    }*/

    /**
     * Ends a unit of work.
     */
/*    public void endWork() {
       EntityManager em = emBinding.get();
       if(em==null)
           throw new IllegalStateException("Cannot end work: no work started");
       try {
        EntityTransaction tx = em.getTransaction();
        if(tx.isActive())
            em.getTransaction().commit();
       } finally {
           if(em.isOpen()) em.close();
           emBinding.unbind();
       }
    }*/

    /**
     * Rolls back a unit of work.
     */
/*    public void undoWork() {
       EntityManager em = emBinding.get();
       if(em==null)
           throw new IllegalStateException("Cannot undo work: no work started");
       try {
            EntityTransaction tx = em.getTransaction();
            if(tx.isActive())
                em.getTransaction().rollback();
       } finally {
          if(em.isOpen()) em.close();
          emBinding.unbind();
       }
    }*/

    public Collection<Execution> getPausedExecutions(Process process, Node node) {
        
        return null;  //TODO
    }
}
